package com.tuda.system.mapper;

import java.math.BigDecimal;
import java.util.List;
import com.tuda.system.domain.TudaOrder;
import com.tuda.system.domain.TudaOrderTransfer;
import com.tuda.system.domain.TudaOrderTree;
import com.tuda.system.model.param.QueryOrderListParam;
import com.tuda.system.model.param.QueryUserOrderListParam;
import com.tuda.system.model.result.NewOrderListResult;
import com.tuda.system.model.result.OrderListResult;
import com.tuda.system.model.result.OrderTInfoResult;
import com.tuda.system.model.result.OrderTotalAmountResult;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 货运订单Mapper接口
 *
 * @author tuda
 * @date 2023-07-14
 */
public interface TudaOrderMapper
{

    List<TudaOrderTree> getOrderTree(String[] ids);

    @Update("update tuda_order set ${feeState}=2, ${feeName}=#{notes}, ${feeAmount}=#{amount} where id=#{orderId}")
    int updateOrderFee(@Param("orderId") Long orderId, @Param("feeState") String feeState, @Param("feeName") String feeName, @Param("notes") String notes, @Param("amount") BigDecimal amount, @Param("feeAmount") String feeAmount);

    List<TudaOrder> queryCompanyOrderListByPhone(TudaOrder tudaOrder);
    List<TudaOrderTransfer> salesmanList(@Param("orderIds") String[] orderIds);

    @Update("update tuda_order set other_fee=#{otherFee} where id=#{orderId}")
    int updateOrderOtherFee(@Param("orderId") Long orderId, @Param("otherFee") BigDecimal otherFee);
    @Update("update tuda_order set order_type=#{orderType} where id=#{orderId}")
    int updateOrderType(@Param("orderId") Long orderId, @Param("orderType") Long orderType);

    @Update("update tuda_order set profit=#{profit}, commission=#{commission} where id=#{orderId}")
    int updateOrderInfo(@Param("orderId") Long orderId, @Param("profit") BigDecimal profit, @Param("commission") BigDecimal commission);

    @Select("select order_id orderId, owes from tuda_order_transfer where id=#{id}")
    TudaOrderTransfer queryInfoById(@Param("id") Long id);

    TudaOrderTransfer queryInfoByValue(@Param("orderId") Long orderId, @Param("transporter") String transporter, @Param("refund") Integer refund);

    @Select("SELECT id, vehicle_inspector vehicleInspector, user_name userName, order_num orderNum, vehicle_model vehicleModel, vin, client, client_phone clientPhone, origin, dest, receiver, " +
            "receiver_phone receiverPhone, pay_type payType, freight_amount freightAmount, paid_amount paidAmount, " +
            "inspection_fee inspectionFee, driving_fee drivingFee, refueling_fee refuelingFee, small_board_fee smallBoardFee, forklift_fee forkliftFee, other_fee otherFee, agency_fee agencyFee, " +
            "car_imgs carImgs, contract_imgs contractImgs, order_type orderType, update_time updateTime, create_time createTime, profit, notes, is_delete isDelete FROM tuda_order WHERE id=#{id}")
    OrderTInfoResult queryOrderInfoById(@Param("id") Long id);

    @Select("SELECT id, order_num orderNum, vehicle_model vehicleModel, vin, client, client_phone clientPhone, origin, dest, receiver, " +
            "receiver_phone receiverPhone, pay_type payType, freight_amount freightAmount, paid_amount paidAmount, " +
            "inspection_fee inspectionFee, driving_fee drivingFee, refueling_fee refuelingFee, small_board_fee smallBoardFee, forklift_fee forkliftFee, other_fee otherFee, agency_fee agencyFee, " +
            "car_imgs carImgs, contract_imgs contractImgs, order_type orderType, update_time updateTime, create_time createTime, profit, notes, is_delete isDelete FROM tuda_order WHERE id=#{id}")
    NewOrderListResult getOrderTransferList(@Param("id") Long id);

    /**
     * 小程序端获取运单列表（业务员）
     * @param param 筛选参数
     * @return
     */
    List<OrderListResult> getList(@Param("param") QueryOrderListParam param);

    /**
     * 小程序端获取运单列表（经办人）
     * @param param 筛选参数
     * @return
     */
    List<OrderListResult> getHandledByList(@Param("param") QueryOrderListParam param, @Param("userPhone") String userPhone);

    /**
     * 小程序端获取运单列表（驾驶员）
     * @param param 筛选参数
     * @return
     */
    List<OrderListResult> getDriverList(@Param("param") QueryOrderListParam param, @Param("userPhone") String userPhone);

    /**
     * 列表总数
     * @param param
     * @return
     */
    int getListCount(@Param("param") QueryOrderListParam param, @Param("userType") Integer userType);

    /**
     * 获取我的-列表数据
     * @param param
     * @return
     */
    List<OrderListResult> getUserList(@Param("param") QueryUserOrderListParam param, @Param("userType") Integer userType);

    /**
     * 我的-列表数据总数
     * @param param
     * @return
     */
    int getUserListCount(@Param("param") QueryUserOrderListParam param, @Param("userType") Integer userType);

    /**
     * 查询货运订单
     *
     * @param id 货运订单主键
     * @return 货运订单
     */
     TudaOrder selectTudaOrderById(Long id);

     @Select("SELECT id, order_num orderNum, vehicle_model vehicleModel, vin, client, client_phone clientPhone, origin, dest, receiver," +
             "receiver_phone receiverPhone, pay_type payType, freight_amount freightAmount, paid_amount paidAmount, car_imgs carImgs, contract_imgs contractImgs," +
             "order_type orderType, update_time updateTime, create_time createTime, profit, notes, is_delete isDelete FROM tuda_order WHERE order_num=#{orderNum}")
     OrderTInfoResult queryOrderByOrderNum(@Param("orderNum") String orderNum);

    /**
     * 财务运单列表
     *
     * @param tudaOrder 货运订单
     * @return 货运订单集合
     */
    List<TudaOrder> queryTudaOrderList(TudaOrder tudaOrder);

    OrderTotalAmountResult selectTotalAmount(TudaOrder tudaOrder);

    /**
     * 查询货运订单列表
     *
     * @param tudaOrder 货运订单
     * @return 货运订单集合
     */
     List<TudaOrder> selectTudaOrderList(TudaOrder tudaOrder);

    /**
     * 新增货运订单
     *
     * @param tudaOrder 货运订单
     * @return 结果
     */
     int insertTudaOrder(TudaOrder tudaOrder);

    /**
     * 修改货运订单
     *
     * @param tudaOrder 货运订单
     * @return 结果
     */
     int updateTudaOrder(TudaOrder tudaOrder);

    /**
     * 删除货运订单
     *
     * @param id 货运订单主键
     * @return 结果
     */
     int deleteTudaOrderById(Long id);

    /**
     * 批量删除货运订单
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
     int deleteTudaOrderByIds(String[] ids);

     /**
     * 更新运单为已删除状态
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
     int updateTudaOrderByIds(String[] ids);

     int updateStatusByIds(String[] ids);

     int updateOrderTypeByIds(String[] ids);

    /**
     * 批量删除货运中转
     *
     * @param ids 需要删除的数据主键集合
     * @return 结果
     */
     int deleteTudaOrderTransferByOrderIds(String[] ids);

    /**
     * 批量新增货运中转
     *
     * @param tudaOrderTransferList 货运中转列表
     * @return 结果
     */
     int batchTudaOrderTransfer(List<TudaOrderTransfer> tudaOrderTransferList);


    /**
     * 通过货运订单主键删除货运中转信息
     *
     * @param id 货运订单ID
     * @return 结果
     */
     int deleteTudaOrderTransferByOrderId(Long id);
}
